package service

import (
	"blog/config"
	"blog/dao"
	"blog/models"
)

func GetPostsByCategoryId(cId, page, pageSize int) (*models.CategoryResponse, error) {
	//上面涉及的数据必须要定义
	//页面上涉及到的所有的数据，必须有定义
	categorys, err := dao.GetAllCategory()
	if err != nil {
		return nil, err
	}
	posts, err := dao.GetPostPageByCategoryId(cId, page, pageSize)
	if err != nil {
		return nil, err
	}
	total, err := dao.CountGetPostByCategoryId(cId)
	if err != nil {
		return nil, err
	}
	pagesCount := (total + pageSize - 1) / pageSize
	var pages []int
	for i := 0; i < pagesCount; i++ {
		pages = append(pages, i+1)
	}
	var hr = &models.HomeResponse{
		config.Cfg.Viewer,
		categorys,
		posts,
		total,
		page,
		pages,
		pagesCount != page,
	}
	CategoryName, err := dao.GetCategoryNameById(cId)
	CategoryResponse := &models.CategoryResponse{
		hr,
		CategoryName,
	}
	return CategoryResponse, nil
}
